package ui;

import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseWheelListener;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.SWT;
public class ScrolledView extends Canvas implements MouseWheelListener{
	
	
	protected int width;
	protected int height;
	protected int scrollHeight = 32;
	protected boolean isWarned = false;
	protected int upcount;
	protected int downcount;
	public ScrolledView(Composite parent, int style) {
		super(parent, style);
		RowLayout rowLayout = new RowLayout(SWT.VIRTUAL);
		rowLayout.wrap = false;
		rowLayout.pack = false;
		rowLayout.spacing = 0;
		rowLayout.marginTop = 0;
		rowLayout.marginLeft = 0;
		rowLayout.marginRight = 0;
		rowLayout.marginBottom = 0;
		setLayout(rowLayout);
		addMouseWheelListener(this);
	}
	
	
	public void setScrollHeight(int height) {
		scrollHeight = height;
	}
	
	@Override
	public void mouseScrolled(MouseEvent arg0) {
		if (arg0.count < 0) {
			scrollUp();
		}else {
			scrollDown();
		}
	}
	
	@Override
	public void setBounds(int x, int y, int width, int height) {
		super.setBounds(x, y, width, height);
		this.width = width;
		this.height = height;
	}
	//已经滚到底了，怎么办
	public void reachBottom() {
		
	}
	
	public void scrollUp() {
		if(downcount > height) {
			scroll(0, upcount -= scrollHeight, 0, upcount + scrollHeight, width, (downcount -= scrollHeight) - upcount, true);
		} else {
			reachBottom();
		}
	}
	
	public void scrollDown() {
		if (upcount < 0) {
			scroll(0, upcount += scrollHeight, 0, upcount - scrollHeight, width, (downcount += scrollHeight) - upcount, true);
		}
	}
	@Override
	public void layout() {
		super.layout();
		scroll(0, upcount, 0, 0, width, downcount - upcount, true);
	}
}
